#include <stdlib.h>
#include "syntax.h"



struct syntax_element_t* create_leaf(struct token_t* token)
{
	struct syntax_element_t* tmp = (struct syntax_element_t*)malloc(sizeof(struct syntax_element_t));
	if(!tmp)
		return NULL;
	tmp->type = TYPE_LEAF;
	tmp->leaf.token = token;
	return tmp;
}

struct syntax_element_t* create_or_node(int num_children)
{
	struct syntax_element_t* tmp = (struct syntax_element_t*)malloc(sizeof(struct syntax_element_t));
	if(!tmp)
		return NULL;
	if(!(tmp->node.children = (struct syntax_element_t**)malloc(sizeof(struct syntax_element_t*)))) {
		free(tmp);
		return NULL;
	}
	tmp->type = TYPE_NODE_OR;
	tmp->node.num_children = num_children;
	return tmp;
}

struct syntax_element_t* create_and_node(int num_children)
{
	struct syntax_element_t* tmp = (struct syntax_element_t*)malloc(sizeof(struct syntax_element_t));
	if(!tmp)
		return NULL;
	if(!(tmp->node.children = (struct syntax_element_t**)malloc(sizeof(struct syntax_element_t*)))) {
		free(tmp);
		return NULL;
	}
	tmp->type = TYPE_NODE_AND;
	tmp->node.num_children = num_children;
	return tmp;
}

void add_to_node(struct syntax_element_t* parent, struct syntax_element_t* child, int num)
{
	parent->node.numchildren[num] = child;
}

